package com.wwwarehouse.taskcenter.fragment.job_point.step2.child;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.ListView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.job_point.WarehousePointAdapter;
import com.wwwarehouse.taskcenter.bean.job_point.WarehousePointResponseBean;
import com.wwwarehouse.taskcenter.constant.job_point.TaskCenterCommon;

import java.util.ArrayList;

/**
 * Created by Jackie on 2017/10/27
 * 子作业点委托权维度  选择添加方式  查询仓库点信息 详情列表
 */

public class ChooseAddWayChildEntrustByWarehousePointDetailsFragment extends BaseFragment {
    private View mRootView;

    private ListView mListView;
    private WarehousePointAdapter mAdapter;

    private ArrayList<WarehousePointResponseBean> mWarehousePointDetailsList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_choose_add_way_child_combination_by_category_details, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initView() {
        mListView = findView(mRootView, R.id.lv_category);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            final int page = bundle.getInt("page");
            mWarehousePointDetailsList = bundle.getParcelableArrayList("dataList");

            mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                    int itemHeight = mListView.getMeasuredHeight() / 10;

                    int start;
                    int end;

                    if ((page - 1) * 10 + 10 < mWarehousePointDetailsList.size()) {
                        start = (page - 1) * 10;
                        end = (page - 1) * 10 + 10;
                    } else {
                        start = (page - 1) * 10;
                        end = mWarehousePointDetailsList.size();
                    }

                    mAdapter = new WarehousePointAdapter(mActivity, R.layout.item_add_way_combination_details, mWarehousePointDetailsList.subList(start, end), itemHeight);

                    mListView.setAdapter(mAdapter);
                }
            });
        }
    }

    private void initEvent() {

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseAddWayChildEntrustByWarehousePointDetailsFragment) {
            if (TaskCenterCommon.getInstance().isDetails()) {
                mActivity.setTitle(R.string.view_details);
            } else {
                if (TaskCenterCommon.getInstance().isStart()) {
                    mActivity.setTitle(R.string.choose_start_value);
                } else {
                    mActivity.setTitle(R.string.choose_end_value);
                }
            }
        }
    }

    @Override
    public void requestDatas() {

    }
}
